1780B - GCD Partition - CodeForces Solution


brute force greedy math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> //find_by_order,order_of_key
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define fr               first
#define sc                second
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll


template<typename T> istream &operator>>(istream &input, vector<T> &v) {for (auto &it : v) input >> it; return input;}
template<typename T> ostream &operator<<(ostream &output, vector<T> &v) {for (auto &it : v) output << it << " "; return output;}
template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;}
template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;}

const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const int N = 2*1e5 + 3;
const int INF = 1e17;
const int dx[4] {1, 0, -1, 0}, dy[4] {0, 1, 0, -1};

#ifndef ONLINE_JUDGE
#include "debug.h"
#else 
#define debug(x)
#endif

void Solve() {
    int n;
    cin >> n;
    vector<int> v(n);
    cin >> v;
    vector<int> prf(n,0),suff(n,0);
    prf[0] = v[0];
    int temp = v[0];
    suff[n-1] = v[n-1];
    for(int i = 1;i<n;i++) temp = __gcd(temp,v[i]),prf[i] = prf[i-1]+v[i];
    for(int i = n-2;i>=0;i--) suff[i] = suff[i+1]+v[i];
    int ans = 1;
    for(int i = 0;i<n-1;i++){
        ans = max(ans,__gcd(prf[i],suff[i+1]));
    }
    cout << max(ans,temp) <<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t = 1;
    cin >> t;
    while (t--) {
        Solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph